'use strict';

const { Controller } = require('ee-core');
const Log = require('ee-core/log');
const Services = require('ee-core/services');
const { success, error } = require('../common');
/**
 * example
 * @class
 */
class ModbusOverTCPController extends Controller {

  constructor(ctx) {
    super(ctx);
    this.service = Services.get('modbusOverTCP');
  }


  /**
   * 所有方法接收两个参数
   * @param args 前端传的参数
   * @param event - ipc通信时才有值。详情见：控制器文档
   */


  async connect (args) {
    try {
      await this.service.connect(args);
      return success();
    } catch (e) {
      return error(e);
    }
  }

  async checkConnect () {
    let res = this.service.checkConnect();
    return res ? success() : error('未连接');
  }

  async close () {
    try {
      await this.service.close();
      return success();
    } catch (e) {
      return error(e);
    }
  }
  async readHolding (args) {
    try {
      let res = await this.service.readHolding(args);
      return success(res);      
    } catch (e) {
      return error(e.message);
    }
  }
  async readCoils (args) {
    try {
      let res = await this.service.readCoils(args);
      return success(res);      
    } catch (e) {
      return error(e.message);
    }
  }
  async readDiscreteInputs (args) {
    try {
      let res = await this.service.readDiscreteInputs(args);
      return success(res);      
    } catch (e) {
      return error(e.message);
    }
  }
  async writeFC5 (args) {
    try {
      let res = await this.service.writeFC5(args);
      return success(res);      
    } catch (e) {
      return error(e.message);
    }
  }
  async writeFC6 (args) {
    try {
      let res = await this.service.writeFC6(args);
      return success(res);      
    } catch (e) {
      return error(e.message);
    }
  }

  async readInput (args) {
    try {
      let res = await this.service.readInput(args);
      return success(res);      
    } catch (e) {
      return error(e.message);
    }
  }

  async send (args) {
    try {
      let res = await this.service.send(args);
      return success(res);      
    } catch (e) {
      return error(e.message);
    }
  }
  
}

ModbusOverTCPController.toString = () => '[class ModbusOverTCPController]';
module.exports = ModbusOverTCPController;  